package com.jqh.androidlockdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.widget.RemoteViews;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RemoteViews contentView;

    private Notification notification;
    private Mp3Application application;
    private IMusic music;

    public static String getDiskPath(Context context) {
        return Environment.getExternalStorageDirectory().getPath();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        application = (Mp3Application) getApplication();
        application.songsList = new ArrayList<>();

        Song song = new Song("1", "mp3_1", getDiskPath(this) + "/1.mp3");
        Song song2 = new Song("2", "mp3_2", getDiskPath(this) + "/2.mp3");
        application.songsList.add(song);
        application.songsList.add(song2);

        NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        application.notManager = manager;

        initNotificationBar();

        startService(new Intent(getBaseContext(), PlayerService.class));

        registerReceiver();
    }

    private void registerReceiver(){
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction("play");
        filter.addAction("last");
        filter.addAction("next");
        filter.addAction("pause");
        filter.addAction("cancel");
        Mp3Receiver mp3Receiver = new Mp3Receiver();
        registerReceiver(mp3Receiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(getBaseContext(), PlayerService.class));
    }

    public void initNotificationBar() {
        Notification notification = new NotificationCompat.Builder(this)
                .setChannelId(application.getApplicationContext().getPackageName())
                .setWhen(System.currentTimeMillis())
                .build();
        //初始化通知
        notification.icon = R.mipmap.ic_launcher;
        contentView = new RemoteViews(getPackageName(),
                R.layout.notification_control);
        notification.contentView = contentView;

        Intent intentPlay = new Intent("play");//新建意图，并设置action标记为"play"，用于接收广播时过滤意图信息
        PendingIntent pIntentPlay = PendingIntent.getBroadcast(this, 0,
                intentPlay, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_play, pIntentPlay);//为play控件注册事件

        Intent intentPause = new Intent("pause");
        PendingIntent pIntentPause = PendingIntent.getBroadcast(this, 0,
                intentPause, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_pause, pIntentPause);

        Intent intentNext = new Intent("next");
        PendingIntent pIntentNext = PendingIntent.getBroadcast(this, 0,
                intentNext, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_next, pIntentNext);

        Intent intentLast = new Intent("last");
        PendingIntent pIntentLast = PendingIntent.getBroadcast(this, 0,
                intentLast, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_last, pIntentLast);

        Intent intentCancel = new Intent("cancel");
        PendingIntent pIntentCancel = PendingIntent.getBroadcast(this, 0,
                intentCancel, 0);
        contentView
                .setOnClickPendingIntent(R.id.bt_notic_cancel, pIntentCancel);
        notification.flags = notification.FLAG_NO_CLEAR;//设置通知点击或滑动时不被清除

        //        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    application.getApplicationContext().getPackageName(),
                    "TAG",
                    NotificationManager.IMPORTANCE_DEFAULT

            );
            application.notManager.createNotificationChannel(channel);
        }
//
//        Notification notification1 = new NotificationCompat.Builder(this)
//                .setContentTitle("1111")
//                .setContentText("2222")
//                .setChannelId(application.getApplicationContext().getPackageName())
//                .setWhen(System.currentTimeMillis())
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setLargeIcon(BitmapFactory.decodeResource(MainActivity.this.getResources(),R.mipmap.ic_launcher))
//                .build();

        application.notManager.notify(1000001, notification);//开启通知
//


    }

}
